#pragma once
#include "../mqcommon/helper.hpp"
#include "../mqcommon/logger.hpp"
#include "../mqcommon/msg.pb.h"
#include <cctype>

class Router
{
public:
    static bool isLegalRoutingKey(const std::string& routingKey)
    {
        for(auto& ch : routingKey)
            if(ch != '.' && ch != '_' && !isalnum(ch)) //&&傻了
                return false;
        return true;
    }
    
    static bool isLegalBindingKey(const std::string& bindingKey)    //其实我认为：#只能出现在末尾
    {
        //1.判断每一个字符
        DLOG("1111");

        for(auto& ch : bindingKey)
            if(ch != '.' && ch != '_' && ch != '#' && ch != '*' && !isalnum(ch)) 
                return false;
        //2.单词中是否有通配符
        DLOG("1111");
        std::vector<std::string> words;
        SplitHelper::split(bindingKey, ".", words);
        for(auto& word : words)
            if(word.size() > 1 && word.find_first_of("*#") != std::string::npos)
                return false;
        DLOG("1111");

        //3.连续的通配符中不能有#
        for(int i = 1; i < words.size(); i++)
            if(words[i - 1].size() == 1 && words[i].size() == 1 && (words[i - 1] == "#" || words[i] == "#"))
                return false;
        return true;
    }

    static bool route(ExchangeType exchangeType, const std::string& routingKey, const std::string& bindingKey)
    {
        if(exchangeType == ExchangeType::DIRECT)
            return routingKey == bindingKey;
        else if(exchangeType == ExchangeType::FANOUT)
            return true;
        else if(exchangeType == ExchangeType::TOPIC)     //aaa.ccc.#     aaa
        {
            // std::vector<std::string> routingKeyWords;   //i
            // SplitHelper::split(routingKey, ".", routingKeyWords);

            // std::vector<std::string> bindingKeyWords;   //j
            // SplitHelper::split(bindingKey, ".", bindingKeyWords);
            // int i = 0, j = 0;
            // while(i < routingKeyWords.size() && j < bindingKeyWords.size())//aaa.#.ddd   aaa.ccc.vvv.ddd这样判断不了
            // {
            //     if(routingKeyWords[i] == bindingKeyWords[j] || bindingKeyWords[j] == "*")
            //         i++, j++;
            //     if(bindingKeyWords[j] == "#")
            //         i++;
            // }
            // if(i != routingKeyWords.size() || j != bindingKeyWords.size() - 1)
            //     return false;
            std::vector<std::string> routingKeyWords;   //j
            SplitHelper::split(routingKey, ".", routingKeyWords);

            std::vector<std::string> bindingKeyWords;   //i
            SplitHelper::split(bindingKey, ".", bindingKeyWords);
            int m = bindingKeyWords.size(), n = routingKeyWords.size();
            std::vector<std::vector<bool>> dp (m + 1, std::vector<bool>(n + 1, false));
            dp[0][0] = true;
            if(bindingKeyWords[0] == "#")
                dp[1][0] = true;
            for(int i = 1; i <= m; i++)
            {
                for(int j = 1; j <= n; j++)
                {
                    if(bindingKeyWords[i - 1] == "#")
                        dp[i][j] = dp[i - 1][j - 1] | dp[i - 1][j] | dp[i][j - 1];
                    else if(routingKeyWords[j - 1] == bindingKeyWords[i - 1] || bindingKeyWords[i - 1] == "*")
                        dp[i][j] = dp[i - 1][j - 1];
                    else dp[i][j] = false;
                }
            }
            return dp[m][n];
        }
        else return false;  //交换机类型不对
    }
};